package com.varian.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.varian.log.annotation.ApiLog;
import com.varian.service.annotation.Resubmit;
import com.varian.service.web.BaseController;
import com.varian.system.dto.SysDictItemDTO;
import com.varian.system.dto.SysDictListDTO;
import com.varian.system.dto.SysDictPageDTO;
import com.varian.system.dto.SysDictTypeDTO;
import com.varian.system.service.ISysDictService;
import com.varian.system.vo.SysDictVO;
import com.varian.tool.result.IR;
import com.varian.tool.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ben
 * @since 2024/7/9
 */
@Resubmit
@RestController
@Tag(name = "字典控制层")
@RequestMapping("/dict")
public class SysDictController extends BaseController<ISysDictService> {

    @ApiLog
    @PostMapping("type")
    @Operation(summary = "新增字典类型")
    @PreAuthorize("hasAuthority('dict:type:add')")
    public IR type(@Validated @RequestBody SysDictTypeDTO dto) {
        return R.status(baseService.typeAdd(dto));
    }

    @ApiLog
    @PostMapping("item")
    @Operation(summary = "新增字典项")
    @PreAuthorize("hasAuthority('dict:item:add')")
    public IR item(@Validated @RequestBody SysDictItemDTO dto) {
        return R.status(baseService.itemAdd(dto));
    }

    @ApiLog
    @GetMapping("{id:\\d+}")
    @Operation(summary = "字典详情")
    @PreAuthorize("hasAuthority('dict:detail')")
    public R<SysDictVO> detail(@PathVariable("id") Long id) {
        return R.data(baseService.detail(id));
    }

    @ApiLog
    @PutMapping("type/{id:\\d+}")
    @Operation(summary = "修改字典类型")
    @PreAuthorize("hasAuthority('dict:type:modify')")
    public IR typeModify(@PathVariable("id") Long id, @Validated @RequestBody SysDictTypeDTO dto) {
        return R.status(baseService.typeModify(id, dto));
    }

    @ApiLog
    @PutMapping("item/{id:\\d+}")
    @Operation(summary = "修改字典项")
    @PreAuthorize("hasAuthority('dict:item:modify')")
    public IR itemModify(@PathVariable("id") Long id, @Validated @RequestBody SysDictItemDTO dto) {
        return R.status(baseService.itemModify(id, dto));
    }

    @ApiLog
    @DeleteMapping("{id:\\d+}")
    @Operation(summary = "删除字典")
    @PreAuthorize("hasAuthority('dict:delete')")
    public IR delete(@PathVariable("id") Long id) {
        return R.status(baseService.delete(id));
    }

    @ApiLog
    @GetMapping("page")
    @Operation(summary = "字典分页")
    @PreAuthorize("hasAuthority('dict:page')")
    public R<Page<SysDictVO>> page(@Validated SysDictPageDTO dto) {
        return R.data(baseService.page(dto));
    }

    @ApiLog
    @GetMapping("list")
    @Operation(summary = "字典列表")
    @PreAuthorize("hasAuthority('dict:list')")
    public R<List<SysDictVO>> list(@Validated SysDictListDTO dto) {
        return R.data(baseService.list(dto));
    }

    @GetMapping("list/{dictCode}")
    @Operation(summary = "根据字典编码查询")
    public R<List<SysDictVO>> listByDictCode(@PathVariable("dictCode") String dictCode) {
        return R.data(baseService.listByDictCode(dictCode));
    }

    @PostMapping("map-by-dict-code")
    @Operation(summary = "根据字段编码查询字典map")
    public R<Map<String,List<SysDictVO>>> mapByDictCode(@RequestBody List<String> dictCodeList) {
        return R.data(baseService.mapByDictCode(dictCodeList));
    }

}
